using Glpt.Common;
using Glpt.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace Glpt.Task
{
    /// <summary>
    /// 下载文件任务
    /// </summary>
    public class DownLoadFileTask
    {
        private BackgroundWorker bwt = null;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public DownLoadFileTask()
        {
        }
        /// <summary>
        /// 测试接口
        /// </summary>
        /// <param name="function_id">功能号</param>
        /// <param name="tradedate">业务日期</param>
        public void SetParams(string function_id, string tradedate)
        {
            SetParams(0, function_id, "", tradedate);
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="db_no">数据库编号</param>
        /// <param name="function_id">功能编号</param>
        /// <param name="task_id">任务编号</param>
        /// <param name="tradedate">业务日期</param>
        public void SetParams(int db_no, string function_id, string task_id, string tradedate)
        {
            BaseInterFace bif = new BaseInterFace(db_no);
            string sql = "SELECT * FROM TB_DOWNLOAD_LIST WHERE TASK_FUNCTION_ID=" + function_id + " AND ENABLE_FLAG='1' ORDER BY DOWN_ID";
            DataTable dt_list = bif.ExecSelect(sql);
            DataRow[] drs = dt_list.Select("DOWN_ID=-1");//动态取
            if (drs.Length > 0)
                dt_list = CommFuns.GetDynamicProc(dt_list, drs[0]["URL"].ToString(), function_id, tradedate);
            Dictionary<string, string> exec_params = new Dictionary<string, string>();
            foreach (DataRow dr in dt_list.Rows)
            {
                if (bwt != null && bwt.CancellationPending)
                    throw new Exception("线程被取消");
                string BUSI_DATE = tradedate;
                string DATE_EXP = Funcs.ReplaceWord(dr["DATE_EXP"].ToString(), BUSI_DATE);
                if (DATE_EXP.Length > 0)
                {
                    CommFuns.AddParams(exec_params, CommFuns.GetDynamicFunc(bif, DATE_EXP, "BUSI_DATE"));
                    if (exec_params.ContainsKey("@BUSI_DATE") && exec_params["@BUSI_DATE"].Length > 0)
                        BUSI_DATE = exec_params["@BUSI_DATE"];
                    if (exec_params.ContainsKey("@BEGIN_DATE") && exec_params["@BEGIN_DATE"].Length == 8 && !exec_params.ContainsKey("@C_BEGIN_DATE"))
                        CommFuns.AddParams(exec_params, "C_BEGIN_DATE", Funcs.StrToDataTime(exec_params["@BEGIN_DATE"]).ToString("yyyy-MM-dd"));
                    if (exec_params.ContainsKey("@END_DATE") && exec_params["@END_DATE"].Length == 8 && !exec_params.ContainsKey("@C_END_DATE"))
                        CommFuns.AddParams(exec_params, "C_END_DATE", Funcs.StrToDataTime(exec_params["@END_DATE"]).ToString("yyyy-MM-dd"));
                }
                string url = Funcs.ReplaceWord(dr["URL"].ToString(), BUSI_DATE);
                string destpath = Funcs.ReplaceWord(dr["DESTPATH"].ToString(), BUSI_DATE);
                foreach (string param_name in exec_params.Keys)
                {
                    url = url.Replace(param_name, Funcs.ReplaceWord(exec_params[param_name], BUSI_DATE));
                    destpath = destpath.Replace(param_name, Funcs.ReplaceWord(exec_params[param_name], BUSI_DATE));
                }
                string host = dr["PROXY_IP"].ToString();
                int port = 0;
                int.TryParse(dr["PROXY_PORT"].ToString(), out port);
                if (url.Length > 0)
                    OpenURL.DownloadFile(url, destpath, host, port);
            }
        }

        /// <summary>
        /// 后台任务对象
        /// </summary>
        public BackgroundWorker BackgroundWorkerTask
        {
            get
            {
                return bwt;
            }
            set
            {
                bwt = value;
            }
        }

        /// <summary>
        /// 释放内存
        /// </summary>
        public void Dispose()
        {
            try
            {
                GC.Collect();
            }
            catch { }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~DownLoadFileTask()
        {
            Dispose();
        }
    }
}
